package com.housediyadmin.view.titlewindow
{   	
	import com.housediyadmin.events.bubbleEvents.UploadDoneEvent;
	import com.housediyadmin.model.ColorModel;
	import com.housediyadmin.model.PicModel;
	import com.housediyadmin.model.SizeModel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/15
	 */ 
	public class AppendGoodsPicMediator extends Mediator
	{
		[Inject]
		public var view:AppendGoodsPicView;
      
		[Inject]
		public var picModel:PicModel;
		
		override public function onRegister():void
		{
			view.currentState = "upload";
			mediatorMap.createMediator(view.uploadPic);
			picModel.selectedFiles.removeAll();
			view.previewBtn.addEventListener(MouseEvent.CLICK, prePopUpHandler);
			view.okBtn.addEventListener(MouseEvent.CLICK, addGoodsHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			view.addEventListener(CloseEvent.CLOSE, closeHandler);
			view.addEventListener(UploadDoneEvent.UPLOAD_DONE, uploadDoneHandler);
		}
		
		private function removeHandler():void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(this);
			
			mediatorMap.removeMediatorByView(view.uploadPic);
		}
		private function prePopUpHandler(event:MouseEvent):void
		{
			/*PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );*/
			removeHandler();
			var popUp:SelectColorView = new SelectColorView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function addGoodsHandler(event:MouseEvent):void
		{
			removeHandler();
			/*PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );*/
		}
		
		private function closeHandler(event:Event):void
		{
			removeHandler();
			/*PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );*/
		}
		
		private function uploadDoneHandler(event:UploadDoneEvent):void
		{
			view.currentState = "done";
			view.showPic.currentState = "append";
			mediatorMap.createMediator(view.showPic);
		}
	}
}